package com.iweb.day11;

import com.iweb.entity.Grade;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WuYongheng
 * @date 2021/11/28
 * @description 查询所以年纪信息
 */
public class Test3 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement statement = null;
        ResultSet resultSet = null;
        // 数据库连接信息
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
        String username = "hr";
        String password = "123456";
        try {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 连接对象
            conn = DriverManager.getConnection(url, username, password);
            if (conn != null) {
                System.out.println("连接成功");
            }
            statement = conn.createStatement();
            String sql = "select * from grade";
            resultSet = statement.executeQuery(sql);
            // 解析返回数据
            List<Grade> gradeList=new ArrayList<>();
            Grade grade=null;
            while (resultSet.next()) {
                // 获取数据（解析并保存数据）
//                System.out.print(resultSet.getInt("gradeid") + "-"
//                        + resultSet.getString("gradeName"));
//                System.out.println();
                grade=new Grade();
                grade.setGradeId(resultSet.getInt("gradeId"));
                grade.setGradeName(resultSet.getString("gradeName"));
                // 保存到集合
                gradeList.add(grade);
            }
            // 查看保存的数据
            for(Grade g:gradeList){
                System.out.println(g.getGradeId()+":"+g.getGradeName());
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
